#######################################################
#A COMMON-SPACE TECHNIQUE FOR VOTERS AND LEGISLATORS
#WITH APPLICATION TO THE 2010/2011 Elections
####################################################### 

##NB - Load the Master file first


##We extract Senators 1&2 and identify unique governors
gov <- paste(V529,"-",V206)

gov_unique <- unique(gov)

##create a new cces variable for house scaling
cces$governor_scale <- NULL


##Now, we go through the data and make a unique column for each governor

#temp_tot is a matrix that will hold all of the data
temp_tot <- NULL

for (i in 1:length(gov_unique)){
  
  temp <- ifelse(gov_unique[i]==gov, CC334B, 999)
  
  temp_tot <- cbind(temp_tot, temp)
  
  if(i==1){
    colnames(temp_tot) <- c(sub(" ", " ", gov_unique)[i])
  }
  
  if(i>1){
    colnames(temp_tot) <- c(colnames(temp_tot)[1:(ncol(temp_tot)-1)], 
                            sub(" ", " ", gov_unique)[i])
  }
}

################################################
#To the matrix of senators, we add individuals,
#parties, and the president
################################################

self <- CC334A
obama <- CC334C
democrats <- CC334D
republicans <- CC334E

placement_matrix <- cbind(self, obama, democrats, republicans, temp_tot)

for (j in 1:ncol(placement_matrix)){
  placement_matrix[,j] <- as.numeric(placement_matrix[,j])
  placement_matrix[,j] <- replace(placement_matrix[,j], placement_matrix[,j]==8 | is.na(placement_matrix[,j]), 999)
}  

###################################################
#Next, we estimate AM step-by-step for each state
###################################################

##We need one overall estimate across all respondents
overall_scale <- aldmck(placement_matrix[,1:4], polarity=2, respondent=1, missing=999, verbose=TRUE)

#save the overall dem and rep pos
dem_overall <- overall_scale$stim[2]
rep_overall <- overall_scale$stim[3]

states_unique <- unique(V206)

##here are the voters' preferences
ideal_points <- overall_scale$resp$ideal


##now governors
placement_holder <- NULL
med_voter <- NULL

for (i in 1:length(states_unique)){
  
  #subset by state
  temp <- subset(placement_matrix, V206==states_unique[i])  
  
  #remove stimuli that are not measured
  drop <- NULL
  for (j in 1:ncol(temp)) if(sum(temp[,j]==999, na.rm=TRUE)==nrow(temp)) drop <- c(drop, j)
  temp <- temp[,-drop]
  
  scaling <- aldmck(temp, polarity=2, respondent=1, missing=999, verbose=TRUE)
  
  #recale from state space back to overall space
  #using the equ OVERALL = slope*STATE - slope*STATE_DEM + OVERALL_DEM
  
  slope <- (overall_scale$stim[3]-overall_scale$stim[2])/(scaling$stim[3]-scaling$stim[2])
  rescaling <- slope*scaling$stim - slope*scaling$stim[2] + overall_scale$stim[2]
  
  #add results to cces dataset...
  cces$governor_scale[V206==states_unique[i]] <- rescaling[4]
  
  #save results
  placement_holder <- c(placement_holder, rescaling[4])
  med_voter <- c(med_voter, mean(ideal_points[V206==states_unique[i]],na.rm=TRUE))
}


##Plot the results
plot(1:length(placement_holder), placement_holder, pch="")
text(1:length(placement_holder), placement_holder, names(placement_holder), cex=0.7, col=4)


##append the state mean preferences
state_ideal_pt <- tapply(ideal_points, V206, mean, na.rm=TRUE)
text(1:length(state_ideal_pt), state_ideal_pt, names(state_ideal_pt), cex=0.7, col=2)


##Group and save the results by state
governor_results <- cbind(placement_holder, med_voter)
